// DlgDisasm.cpp : implementation file
//
#include "stdafx.h"
#include "dlltool.h"
#include "DlgDisasm.h"
#include "../common/src/disasm/olydisasm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlgDisasm dialog


CDlgDisasm::CDlgDisasm(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgDisasm::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgDisasm)
	m_csResult = _T("");
	m_csFileName = _T("C:\\Downloads\\software\\odbg110\\Cmdline.dll");
	//}}AFX_DATA_INIT
}


void CDlgDisasm::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgDisasm)
	DDX_Text(pDX, IDC_EDIT_RESULT, m_csResult);
	DDX_Text(pDX, IDC_EDIT_FILE_NAME, m_csFileName);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgDisasm, CDialog)
	//{{AFX_MSG_MAP(CDlgDisasm)
	ON_BN_CLICKED(IDC_BN_OPEN_FILE, OnBnOpenFile)
	ON_BN_CLICKED(IDC_BN_DIS_TEST, OnBnDisTest)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgDisasm message handlers

void CDlgDisasm::OnBnOpenFile() 
{
	// TODO: Add your control notification handler code here
	uint8 * buffer = 0;
	UpdateData();
	CPEInfo piFile;
	piFile.SetFilePath(m_csFileName.GetBuffer(0));
	if(!piFile.ReadFile())
	{
		return;
	}
	piFile.ReadCodeSection(buffer);
	bool flag = true;
	DWORD nRVA = 0 ;
	DWORD pbuffer = 0;
	t_disasm da;
	IMAGE_NT_HEADERS data;
	piFile.GetNtHead(data);
	for(;flag;)
	{
		pbuffer = (DWORD)(buffer) + nRVA;
	  nRVA+=Disasm((char *)pbuffer,13,(ulong)pbuffer,&da,DISASM_CODE);
		m_csResult += CString(da.result);
		m_csResult += CString("\r\n");
		if(nRVA >data.OptionalHeader.SizeOfCode - 13)
		{
			flag = false;
		}
	}
 	free(buffer);
	UpdateData(FALSE);
}

void CDlgDisasm::OnBnDisTest() 
{
	// TODO: Add your control notification handler code here
	t_disasm da;
	char data[20]= 
		//"\0";
		"\xff\x15";
	Disasm(data,13,(ulong)0X4000,&da,DISASM_CODE);
	
}
